package com.yuelao.zookeeper;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

/**
 * Description
 *
 * @author fengliye
 * @version v1.0
 * @date 26/12/2018 16:43
 * @modify detail
 * 版本       修改人        修改时间      修改内容描述
 * ---------------------------------------------
 * <p>
 * ---------------------------------------------
 */
public class ZKExists {
    private static ZooKeeper zk;
    private static ZooKeeperConn conn;

    // Method to check existence of znode and its status, if znode is available.
    public static Stat znode_exists(String path) throws
            KeeperException,InterruptedException {
        return zk.exists(path, true);
    }

    public static void main(String[] args) throws InterruptedException,KeeperException {
        String path = "/Node_feng"; // Assign znode to the specified path

        try {
            conn = new ZooKeeperConn();
            zk = conn.connect("localhost");
            Stat stat = znode_exists(path); // Stat checks the path of the znode

            if(stat != null) {
                System.out.println("Node exists and the node version is " +
                        stat.getVersion());
            } else {
                System.out.println("Node does not exists");
            }

        } catch(Exception e) {
            System.out.println(e.getMessage()); // Catches error messages
        }
    }
}
